package com.lianliankan.llkan;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.EditText;
import android.widget.ImageButton;

import com.example.llkan.R;
import com.lianliankan.model.Users;
import com.lianliankan.util.ButtonSelector;
import com.lianliankan.util.UserAsyncTask;

public class LoginActivity extends Activity {
	private ImageButton actionButton;
	private EditText accountEditText;
	private EditText passwordEditText;
	private EditText confrimPasswordEditText;
	private EditText nickNameEditText;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		Intent intent = getIntent();
		String action = intent.getStringExtra("action");
		if (action.equals("login")) {
			setContentView(R.layout.activity_login);
			loginFindView();
			loginInit();
		} else if (action.equals("register")) {
			setContentView(R.layout.activity_register);
			registerFindView();
			registerInit();
		}

	}

	private void loginFindView() {
		actionButton = (ImageButton) findViewById(R.id.loginButton);
		accountEditText = (EditText) findViewById(R.id.accountLoginText);
		passwordEditText = (EditText) findViewById(R.id.passwordLoginText);
	}

	private void loginInit() {
		ButtonSelector bSelector = new ButtonSelector();

		actionButton.setBackgroundDrawable(bSelector.newSelector(
				LoginActivity.this, R.drawable.loginbtn, R.drawable.loginpress,
				R.drawable.loginpress, R.drawable.loginpress));

		actionButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				Users user = new Users();
				user.setAccount(accountEditText.getText().toString());
				user.setPasswrod(passwordEditText.getText().toString());
				UserAsyncTask userAsyncTask = new UserAsyncTask(user, "login",
						LoginActivity.this);
				userAsyncTask.execute();
			}
		});
	}

	private void registerFindView() {
		actionButton = (ImageButton) findViewById(R.id.registerButton);
		accountEditText = (EditText) findViewById(R.id.accountRegisterText);
		passwordEditText = (EditText) findViewById(R.id.passwordRegisterText);
		nickNameEditText = (EditText) findViewById(R.id.nickRegisterText);
		confrimPasswordEditText = (EditText) findViewById(R.id.confirmPasswordRegisterText);
	}

	private void registerInit() {
		ButtonSelector bSelector = new ButtonSelector();

		actionButton.setBackgroundDrawable(bSelector.newSelector(
				LoginActivity.this, R.drawable.registerbtn,
				R.drawable.registerpress, R.drawable.registerpress,
				R.drawable.registerpress));

		actionButton.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				String confirmPassword = confrimPasswordEditText.getText()
						.toString();
				String password = passwordEditText.getText().toString();
				if (confirmPassword.equals(password)) {
					Users user = new Users();
					user.setAccount(accountEditText.getText().toString());
					user.setPasswrod(password);
					user.setNickName(nickNameEditText.getText().toString());
					UserAsyncTask userAsyncTask = new UserAsyncTask(user,
							"register", LoginActivity.this);
					userAsyncTask.execute();
				} else {
					AlertDialog.Builder alert = new AlertDialog.Builder(
							LoginActivity.this);
					alert.setTitle("两次密码输入不一致！");
					alert.show();
				}
			}
		});
	}

	protected void onResume() {
		if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE) {
			setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
		}
		super.onResume();
	}
}
